﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminGuideController : BaseController
    {
        //
        // GET: /AdminGuide/
        [UrlStatFilter]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MenuListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = CourseGuideMenuBLL.GetList();
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [UrlStatFilter]
        public ActionResult MenuSub(int id)
        {
            var info = CourseGuideMenuBLL.Get(id);
            if (info == null)
                return View("NotFound");
            return View(info);
        }

        public ActionResult MenuSubEdit(int? id, int? cid, string ran)
        {
            ViewData["ranStr"] = ran;
            if (string.IsNullOrEmpty(ran))
            {
                var ranStr = RandomStr.GetByTime(UserHelper.LoginUser.Id + "_" + (byte)UserHelper.LoginUser.UserType + "_");
                ViewData["ranStr"] = ranStr;
                if (string.IsNullOrEmpty(ranStr))
                    return View("NotFound");
            }
            else
            {
                if (!RandomStr.Check(ran))
                    return View("NotFound");
            }
            if (id.HasValue)
            {
                var info = CourseGuideSubMenuBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return View(info);
            }
            if (cid.HasValue)
                return View(new CourseGuideSubMenuInfo { MenuId = cid.Value });
            return View("NotFound");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult MenuSubEdit(CourseGuideSubMenuInfo info, string ran, int cid)
        {
            ViewData["ranStr"] = ran;
            if (!RandomStr.Check(ran))
                return View("NotFound");
            ViewData.Model = info;
            if (!ModelState.IsValid)
            {
                return View();
            }
            if (info.Id != 0)
            {
                var oldInfo = CourseGuideSubMenuBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.Name = info.Name;
                oldInfo.CommonContentInfo.DetailContent = (info.CommonContentInfo.DetailContent ?? "");
                if (CommonContentBLL.Edit(oldInfo.CommonContentInfo) && CourseGuideSubMenuBLL.Edit(oldInfo))
                {
                    AttachmentsHelper.ClearUselessAttach(oldInfo.CommonContentInfo.DetailContent, ran, oldInfo.ContentId);
                    ViewData["AlertMsg"] = "编辑成功.";
                    return View(oldInfo);
                }
                ViewData["AlertMsg"] = "未知错误.";
                return View();
            }
            if (cid == 0)
            {
                ViewData["AlertMsg"] = "参数丢失,请重新添加.";
                return View();
            }
            info.CommonContentInfo.DetailContent = info.CommonContentInfo.DetailContent ?? "";
            info.MenuId = cid;
            if (CourseGuideSubMenuBLL.Add(info))
            {
                AttachmentsHelper.ClearUselessAttach(info.CommonContentInfo.DetailContent, ran, info.ContentId);
                TempData["AlertMsg"] = "添加成功.";
                return RedirectToAction("MenuSubEdit", new { id = info.Id, ran = ran });
            }
            ViewData["AlertMsg"] = "未知错误.";
            return View();
        }

        public ActionResult MenuSubDel(int id)
        {
            var info = CourseGuideSubMenuBLL.Get(id);
            if (info != null)
            {
                var toDelAttachs = info.CommonContentInfo.AttachmentsInfo.ToList();
                if (CourseGuideSubMenuBLL.Del(id))
                {
                    AttachmentsBLL.DelFileByList(toDelAttachs);
                    SetAlertMsg("删除成功.", true);
                }
                return RedirectToAction("MenuSub", new { id = info.MenuId });
            }
            return NotFound();
        }

        [UrlStatFilter]
        public ActionResult UserList()
        {
            return View();
        }

        public ActionResult UserListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = TeacherBLL.GetAllTeacher().AsQueryable();
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult UserEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = TeacherBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return View(info);
            }
            return View(new TeacherInfo { Account = "", Name = "" });
        }

        [HttpPost]
        [UserAuth(UserType = UserTypeEnum.Admin)]
        public ActionResult UserEdit(TeacherInfo info)
        {
            info.Titles = info.Titles ?? "";
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return View();
            if (info.Id == 0)
            {
                SetErrorMsg("参数错误:ID");
                return NotFound();
            }
            var fileName = "";
            if (info.Type != 0 && info.Type != 4)
            {
                var msg = "";
                fileName = ImgHelper.UploadImg(Request.Files["UpFile"], ResourcesHelper.UploadImgSize,
                                                               150, 150, out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    SetAlertMsg(msg);
                    return View();
                }
                if (info.Id == 0 && string.IsNullOrEmpty(fileName))
                {
                    ModelState.AddModelError("Img", "请选择上传相片.");
                    return View();
                }
            }
            var oldInfo = TeacherBLL.Get(info.Id);
            if (oldInfo == null)
                return NotFound();

            oldInfo.Titles = info.Titles;
            info.Img = fileName;
            if (!string.IsNullOrEmpty(info.Img))
                oldInfo.Img = info.Img;
            oldInfo.Introduction = info.Introduction??"";

            if (TeacherBLL.Edit(oldInfo))
            {
                SetAlertMsg("编辑成功.");
                return View(oldInfo);
            }
            SetAlertMsg("未知错误.");
            return View();
        }

        public ActionResult MenuSubListUc(int id, int? pid)
        {
            CommonRequest.ClearCache();
            var list = CourseGuideSubMenuBLL.GetList(id).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }
        [UrlStatFilter]
        public ActionResult Notice()
        {
            return View();
        }

        public ActionResult NoticeListUc(int? pid, string search, string startDate, string endDate)
        {
            CommonRequest.ClearCache();
            var list = CourseNoticeBLL.GetList(search, startDate.CheckNoDatetime(), endDate.CheckNoDatetime()).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult NoticeEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = CourseNoticeBLL.Get(id.Value);
                if (info == null)
                    return View("NotFound");
                return PartialView(info);
            }
            return PartialView(new CourseNoticeInfo { NoticeContent = "" });
        }

        [HttpPost]
        public ActionResult NoticeEdit(CourseNoticeInfo info)
        {
            if (!ModelState.IsValid)
                return PartialView(info);
            if (info.Id != 0)
            {
                var oldInfo = CourseNoticeBLL.Get(info.Id);
                if (oldInfo == null)
                    return View("NotFound");
                oldInfo.NoticeContent = info.NoticeContent ?? "";
                oldInfo.NoticeTime = DateTime.Now;
                if (CourseNoticeBLL.Edit(oldInfo))
                {
                    ViewData["msg"] = "编辑成功.";
                    return PartialView(oldInfo);
                }
                ViewData["msg"] = "未知错误.";
                return PartialView(info);
            }

            info.NoticeTime = DateTime.Now;
            if (CourseNoticeBLL.Add(info))
            {
                ViewData["msg"] = "编辑成功.";
                return PartialView(info);
            }
            ViewData["msg"] = "未知错误.";
            return PartialView(info);
        }

        public ActionResult NoticeDel(int id)
        {
            if (CourseNoticeBLL.Del(id))
            {
                TempData["AlertMsg"] = "删除成功.";
                return RedirectToAction("Notice");
            }
            TempData["AlertMsg"] = "未知错误.";
            return RedirectToAction("Notice");
        }
    }
}
